package cn.iocoder.devlop.module.annual.report.marpripinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.devlop.framework.common.pojo.PageResult;
import cn.iocoder.devlop.module.annual.report.marpripinfo.controller.vo.CheckResp;
import cn.iocoder.devlop.module.annual.report.marpripinfo.controller.vo.MarpripinfoBaseVO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.controller.vo.MarpripinfoExportReqVO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.controller.vo.MarpripinfoPageReqVO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.convert.MarpripinfoConvert;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.MarpripinfoMapper;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.MarpripinfoTPMapper;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.devlop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.MARPRIPINFO_NOT_EXISTS;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.MARPRIPINFO_STATE_ERROR;

/**
 * 企业基本信息 Service 实现类
 *
 * @author sunhao
 */
@Service
@Validated
public class MarpripinfoServiceImpl implements MarpripinfoService {

    @Resource
    private MarpripinfoMapper marpripinfoMapper;

    @Resource
    private MarpripinfoTPMapper marpripinfoTPMapper;

    @Override
    public String createMarpripinfo(MarpripinfoBaseVO createReqVO) {
        // 插入
        MarpripinfoDO marpripinfo = MarpripinfoConvert.INSTANCE.convert(createReqVO);
        marpripinfoMapper.insert(marpripinfo);
        // 返回
        return marpripinfo.getId();
    }

    @Override
    public void updateMarpripinfo(MarpripinfoBaseVO updateReqVO) {
        // 校验存在
        validateMarpripinfoExists(updateReqVO.getId());
        // 更新
        MarpripinfoDO updateObj = MarpripinfoConvert.INSTANCE.convert(updateReqVO);
        marpripinfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMarpripinfo(String id) {
        // 校验存在
        validateMarpripinfoExists(id);
        // 删除
        marpripinfoMapper.deleteById(id);
    }

    private void validateMarpripinfoExists(String id) {
        if (marpripinfoMapper.selectById(id) == null) {
            throw exception(MARPRIPINFO_NOT_EXISTS);
        }
    }

    @Override
    public MarpripinfoDO getMarpripinfo(String regno) {
//        return getMarpripinfoFromAll(regno);

        return marpripinfoMapper.selectByRegno(regno);
    }

    @Override
    public List<MarpripinfoDO> getMarpripinfoList(Collection<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return marpripinfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MarpripinfoDO> getMarpripinfoPage(MarpripinfoPageReqVO pageReqVO) {
        return marpripinfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MarpripinfoDO> getMarpripinfoList(MarpripinfoExportReqVO exportReqVO) {
        return marpripinfoMapper.selectList(exportReqVO);
    }

    @Override
    public CheckResp checkMarpripinfo(String regno) {
        // 确认这个企业是否存在并确认这个企业的是否有联络员
        // TODO
        return marpripinfoMapper.checkMarpripinfo(regno);
    }

    public void checkState(String regno){
        MarpripinfoDO marpripinfo = getMarpripinfo(regno);
        if (null == marpripinfo){
            throw exception(MARPRIPINFO_NOT_EXISTS);
        }

        if (!marpripinfo.getOpstate().equals("06") && !marpripinfo.getOpstate().equals("97")){
            throw exception(MARPRIPINFO_STATE_ERROR);
        }

    }

    @Override
    public List<MarpripinfoDO> getChildren(String regno) {
        return marpripinfoMapper.selectChildren(regno);
    }

//    @Override
//    @DS("tp")
//    public MarpripinfoDO getMarpripinfoFromTP(String regno) {
//        LambdaQueryWrapperX<MarpripinfoTPDO> marpripinfoTPDOLambdaQueryWrapper = new LambdaQueryWrapperX<>();
//        marpripinfoTPDOLambdaQueryWrapper.eq(MarpripinfoTPDO::getRegno,regno);
//        MarpripinfoTPDO marpripinfoTPDO = marpripinfoTPMapper.selectOne(marpripinfoTPDOLambdaQueryWrapper);
//        return BeanUtil.toBean(marpripinfoTPDO, MarpripinfoDO.class);
//    }
//
//    @Override
//    public MarpripinfoDO getMarpripinfoFromAll(String regno){
//        MarpripinfoDO marpripinfoFromTP = getMarpripinfoFromTP(regno);
//        MarpripinfoDO marpripinfo = getMarpripinfo(regno);
//        // 双方都为空时返回null，双方都有时比较时间戳，拓普有我们没有我们增加，拓普没有我们有进行删除
//        if(ObjUtil.isEmpty(marpripinfo) && ObjUtil.isEmpty(marpripinfoFromTP)){
//            return null;
//        }else if (ObjUtil.isNotEmpty(marpripinfo) && ObjUtil.isNotEmpty(marpripinfoFromTP)){
//            marpripinfoMapper.updateById(marpripinfoFromTP);
//            return marpripinfoFromTP;
//        }else if (ObjUtil.isEmpty(marpripinfo) && ObjUtil.isNotEmpty(marpripinfoFromTP)){
//            marpripinfoMapper.insert(marpripinfoFromTP);
//            return marpripinfoFromTP;
//        }else{
//            LambdaQueryWrapperX<MarpripinfoDO> marpripinfoDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
//            marpripinfoDOLambdaQueryWrapperX.eq(MarpripinfoDO::getRegno,regno);
//            marpripinfoMapper.delete(marpripinfoDOLambdaQueryWrapperX);
//            return null;
//        }
//    }
}
